package org.kzwl.book.controller.web;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.system.SystemUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sun.management.OperatingSystemMXBean;
import io.swagger.annotations.Api;
import org.kzwl.comm.pojo.ResponseData;
import org.kzwl.comm.pojo.bo.AdminQueryCuVolunteersBO;
import org.kzwl.entity.web.CuData;
import org.kzwl.entity.web.CuVisit;
import org.kzwl.entity.web.CuVolunteers;
import org.kzwl.service.web.CuDataService;
import org.kzwl.service.web.CuVisitService;
import org.kzwl.service.web.CuVolunteersService;
import org.kzwl.service.web.ThearService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.lang.management.ManagementFactory;
import java.math.BigDecimal;
import java.util.*;

@Api("大数据")
@RestController
@RequestMapping("/cu-data")
public class CuDataController {

    @Autowired
    CuDataService cuDataService;
    @Autowired
    ThearService thearService;
    @Autowired
    CuVisitService cuVisitService;
    @Autowired
    CuVolunteersService cuVolunteersService;

    /**
     * 获取访问量
     *
     * @return
     */
    @GetMapping("/visit")
    public ResponseData<Map<String, Object>> saveOne() {
        Map<String, Object> map = new HashMap<>();
        map.put("today", cuDataService.getToday().getCuDataVisit());
        map.put("total", cuDataService.getMap(new QueryWrapper<CuData>().select("sum(cu_data_visit) as sumall")).get("sumall"));
        map.put("near", cuDataService.getMap(new QueryWrapper<CuData>().select("sum(cu_data_visit) as sumall").gt("cu_data_date", DateUtil.offsetDay(new Date(), -15))).get("sumall"));
        map.put("mouth", cuDataService.getMap(new QueryWrapper<CuData>().select("sum(cu_data_visit) as sumall").gt("cu_data_date", DateUtil.beginOfMonth(new Date()))).get("sumall"));
        return new ResponseData<>(map);
    }

    /**
     * 获取大数据中间模块
     */
    @GetMapping("/getCenter")
    public ResponseData<Map> getCpu() {
        Map map = new HashMap<>();
        long totalMemory = SystemUtil.getTotalMemory();
        long freeMemory = SystemUtil.getFreeMemory();
        map.put("memory", NumberUtil.div(freeMemory, totalMemory, 2) * 100);

        OperatingSystemMXBean osmxb = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
        double cpuLoad = osmxb.getSystemCpuLoad();
        int cpu = (int) (cpuLoad * 100);
        map.put("cpu", cpu);
        map.put("register", cuDataService.getMap(new QueryWrapper<CuData>().select("sum(cu_data_register) as sumall")).get("sumall"));
        map.put("login", cuDataService.getMap(new QueryWrapper<CuData>().select("sum(cu_data_register) as sumall")).get("sumall"));
        map.put("bookVisit", cuDataService.getMap(new QueryWrapper<CuData>().select("sum(cu_data_bookVisit) as sumall")).get("sumall"));
        map.put("fengcai", thearService.count());
        //志愿者
        return new ResponseData<>(map);
    }

    @GetMapping("/model")
    public ResponseData<List<Double>> model() {
        List<Double> list = new LinkedList<>();
        CuVisit visit = cuVisitService.getCuVisit();
        Integer index = visit.getCuVisitIndex();
        Integer feiyi = visit.getCuVisitFeiyi();
        Integer fengcai = visit.getCuVisitFengcai();
        Integer lvyou = visit.getCuVisitLvyou();
        Integer news = visit.getCuVisitNews();
        Integer read = visit.getCuVisitRead();
        Integer tougao = visit.getCuVisitTougao();
        Integer zhibo = visit.getCuVisitZhibo();
        Integer yuyue = visit.getCuVisitYuyue();
        Integer zhiyuanzhe = visit.getCuVisitZhiyuanzhe();
        Integer max = NumberUtil.max(new Integer[]{index, feiyi, fengcai, lvyou, news, read, tougao, zhibo, yuyue, zhiyuanzhe});
        double beishu = NumberUtil.div(100, max * 1.0);
        list.add(index * beishu);
        list.add(news * beishu);
        list.add(read * beishu);
        list.add(tougao * beishu);
        list.add(fengcai * beishu);
        list.add(feiyi * beishu);
        list.add(zhibo * beishu);
        list.add(yuyue * beishu);
        list.add(lvyou * beishu);
        list.add(zhiyuanzhe * beishu);
        return new ResponseData<>(list);
    }

    /**
     * 访问监控
     *
     * @return
     */
    @GetMapping("/spaceVisit")
    public ResponseData<List<Integer>> spaceVisit() {
        List<Integer> list = new LinkedList<>();
        CuVisit cuVisit = cuVisitService.getCuVisit();
        list.add(cuVisit.getCuVisitSiteCentre());
        list.add(cuVisit.getCuVisitSiteCulture());
        list.add(cuVisit.getCuVisitSiteBook());
        list.add(cuVisit.getCuVisitSitePainting());
        return new ResponseData<>(list);
    }

    /**
     * 访问监控
     *
     * @return
     */
    @GetMapping("/userTongJi")
    public ResponseData<Map> userTongJi() {
        //查询近15天数据
        Map map = new HashMap<>();
        //查询近15天数据Map map = new HashMap<>();
        List<Integer> listRead = new LinkedList<>();
        List<Integer> listVisit = new LinkedList<>();
        List<String> listDate = new LinkedList<>();
        for (int i = -14; i <= 0; i++) {
            DateTime dateTime = DateUtil.beginOfDay(DateUtil.offsetDay(new Date(), i));
            Map<String, Object> objectMap = cuDataService.getMap(new QueryWrapper<CuData>().select("sum(cu_data_visit) as visit,sum(cu_data_bookVisit) as bookVisit").le("cu_data_date", dateTime));
            int visit = 0;
            int read = 0;
            if (objectMap != null) {
                visit = ((BigDecimal) objectMap.get("visit")).intValue();
                read = ((BigDecimal) objectMap.get("bookVisit")).intValue();
            }
            listVisit.add(visit);
            listRead.add(read);
            listDate.add(dateTime.getField(Calendar.DATE) + "日");
        }
        map.put("read", listRead);
        map.put("visit", listVisit);
        map.put("date", listDate);
        return new ResponseData<>(map);
    }

    /**
     * 获取志愿者活动
     *
     * @return
     */
    @GetMapping("/activity")
    public ResponseData<IPage<CuVolunteers>> activity() {
        AdminQueryCuVolunteersBO adminQueryCuVolunteersBO = new AdminQueryCuVolunteersBO();
        adminQueryCuVolunteersBO.setCurrent(1);
        adminQueryCuVolunteersBO.setSize(7);
        return cuVolunteersService.queryPage(adminQueryCuVolunteersBO);
    }

    /**
     * 志愿者每日注册
     */
    @GetMapping("/volunteer")
    public ResponseData<List<Map<String, Object>>> volunteer() {
        IPage<CuData> cuDataPage = new Page<>(1, 15);//参数一是当前页，参数二是每页个数
        List<Map<String, Object>> list = new ArrayList<>();
        IPage<CuData> page = cuDataService.page(cuDataPage, new LambdaQueryWrapper<CuData>().orderByDesc(CuData::getCuDataId));
        for (CuData cuData : page.getRecords()) {
            HashMap<String, Object> map = new HashMap<>();
            map.put("name", DateUtil.formatChineseDate(cuData.getCuDataDate(), false));
            map.put("value", cuData.getCuDataVolunteer());
            list.add(map);
        }
        return new ResponseData<>(list);
    }
}